//
//  CYHttpTool.m
//  CreditAddressBook
//
//  Created by Lee on 15/4/24.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYHttpTool.h"
#import "AFNetworking.h"
#import "CYUser.h"
#import "CYLoginHttpTool.h"

@implementation CYHttpTool

/**
 *  发送一个get请求
 */
+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure {
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 30.f;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //2.发送请求
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

/**
 *  发送一个10086请求
 */
+ (void)get10086WithURL:(NSString *)url params:(NSDictionary *)params success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure {
     //1.创建请求管理对象
     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     [manager.requestSerializer setValue:@"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0" forHTTPHeaderField:@"User-Agent "];
     [manager.requestSerializer setValue:@"application/json, text/javascript, */*; q=0.01" forHTTPHeaderField:@"Accept"];
     [manager.requestSerializer setValue:@"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" forHTTPHeaderField:@"Accept-Language"];
     [manager.requestSerializer setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
     [manager.requestSerializer setValue:@"https://login.10086.cn/" forHTTPHeaderField:@"Referer"];
     manager.requestSerializer.timeoutInterval = 30.f;
     manager.requestSerializer = [AFHTTPRequestSerializer serializer];
     manager.responseSerializer = [AFHTTPResponseSerializer serializer];
     //2.发送请求
     [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
          if (success) {
               success(responseObject);
          }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if (failure) {
               failure(error);
          }
     }];
}

/**
 *  发送一个10010请求
 */
+ (void)get10010WithURL:(NSString *)url params:(NSDictionary *)params success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure {
     //1.创建请求管理对象
     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     manager.requestSerializer.timeoutInterval = 30.f;
     manager.requestSerializer = [AFHTTPRequestSerializer serializer];
     manager.responseSerializer = [AFHTTPResponseSerializer serializer];
     //2.发送请求
     [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
          if (success) {
               success(responseObject);
          }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if (failure) {
               failure(error);
          }
     }];
}

/**
 *  发送一个189请求
 */
+ (void)get189WithURL:(NSString *)url params:(NSDictionary *)params success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure {
     //1.创建请求管理对象
     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     [manager.requestSerializer setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
     [manager.requestSerializer setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];
     [manager.requestSerializer setValue:@"zh-CN,zh;q=0.8" forHTTPHeaderField:@"Accept-Language"];
     [manager.requestSerializer setValue:@"max-age=0" forHTTPHeaderField:@"Cache-Control"];
     [manager.requestSerializer setValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
     [manager.requestSerializer setValue:@"121" forHTTPHeaderField:@"Content-Length"];
     [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

     manager.requestSerializer.timeoutInterval = 30.f;
     manager.requestSerializer = [AFHTTPRequestSerializer serializer];
     manager.responseSerializer = [AFHTTPResponseSerializer serializer];
     //2.发送请求
     [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
          if (success) {
               success(responseObject);
          }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if (failure) {
               failure(error);
          }
     }];
}



/**
 *  发送一个Post请求
 */
+ (AFHTTPRequestOperation *)postWithURL:(NSString *)url params:(NSDictionary *)params success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure {
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 30.f;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //2.发送请求
    AFHTTPRequestOperation *operation = [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
    return operation;
}


/**
 *  发送一个Post请求
 */
+ (AFHTTPRequestOperation *)postWithURL:(NSString *)url params:(NSDictionary *)params success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure uploadProgress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block {
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 30.f;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    //2.发送请求
    AFHTTPRequestOperation *operation = [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
    //获取上传进度
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        //        CYLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
        if (block) {
            block(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
        }
    }];
    
    return operation;
}

/**
 *  发送一个get请求
 */
+ (void)getSerializerWithURL:(NSString *)url params:(NSDictionary *)params success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure {
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 30.f;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //2.发送请求
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
/**
 *  发送一个Post Serializer请求
 */
+ (void)postSerializerWithURL:(NSString *)url params:(NSDictionary *)params success:(HttpRequestSuccess1)success failure:(HttpRequestFailure)failure {
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 30.f;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //2.发送请求
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params image:(UIImage *)image success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure uploadProgress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block {
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
//    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.requestSerializer.timeoutInterval = 40.f;
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //2.发送请求
     CYUser *user = [CYLoginHttpTool userData].user;
     NSInteger myInteger = [user.percent integerValue];
    AFHTTPRequestOperation *operation = [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
         
        NSData *data = UIImageJPEGRepresentation(image, myInteger);
        [formData appendPartWithFileData:data name:@"file" fileName:@"a.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        sleep(2);
        if (success) {
//            NSData *responseObjectData = responseObject;
//            NSString *json =  [[NSString alloc]initWithData:responseObjectData encoding:NSUTF8StringEncoding];
            
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
    //获取上传进度
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
//        CYLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
        if (block) {
            block(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
        }
    }];

//    [operation setDownloadProgressBlock:];
}

//云同步图片上传
+ (void)postCloudWithURL:(NSString *)url params:(NSDictionary *)params image:(UIImage *)image success:(HttpRequestSuccess)success failure:(HttpRequestFailure)failure uploadProgress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block {
    //1.创建请求管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.requestSerializer.timeoutInterval = 40.f;
     CYUser *user = [CYLoginHttpTool userData].user;
     NSInteger myInteger = [user.percent integerValue];
    NSString *fileName = [NSString stringWithFormat:@"%@,%@.jpg", params[@"userId"],params[@"hashCode"]];
    //2.发送请求
    AFHTTPRequestOperation *operation = [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSData *data = UIImageJPEGRepresentation(image, myInteger);
        [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        sleep(2);
        if (success) {
            //            NSData *responseObjectData = responseObject;
            //            NSString *json =  [[NSString alloc]initWithData:responseObjectData encoding:NSUTF8StringEncoding];
            
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
    //获取上传进度
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        //        CYLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
        if (block) {
            block(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
        }
    }];
    
    //    [operation setDownloadProgressBlock:];
}

+ (NSDictionary *)addVersion:(NSDictionary *)dic {
    NSMutableDictionary *para = [NSMutableDictionary dictionaryWithDictionary:dic];
    if (para[@"version"] == nil) {
        [para setObject:@"1.00" forKey:@"version"];
    }
    return para;
}
@end
